<?php
namespace Home\Controller;
use Think\Page;
class IndexController extends HomeController {
    public function index(){
        //最新发布的new 8篇文章
        $Article =  D('Article');

        $count = $Article->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new Page($count,3);// 实例化分页类 传入总记录数
        // 进行分页数据查询
        $new = $Article->news($Page);
        $show = $Page->show();// 分页显示输出

        //把表标签  分开 如 '你好,小二'  分成 array('你好','小二');
        $new = $Article->tag($new,$flag = true);
        //调用时间格式化
        $new = $Article->timeFormat($new);

        //建立一个字段
        $new = $Article->htmlTag($new);

        $this->assign('page',$show);// 赋值分页输出
        $this->assign('new',$new);// 赋值数据集
        $this->display();
    }

    public function article() {
        if (IS_GET) {
            $id = I('get.id');
            $Article =  D('Article');

            //取得一条文章详细内容
            $Article->detail();
            $detail = $Article->detail($id);

            if ($detail) {
                //把表标签  分开 如 '你好,小二'  分成 array('你好','小二');
                $detail['art_tag'] = $Article->tag($detail['art_tag']);
                //调用时间格式化
                $detail = $Article->timeFormat($detail,$flag=true);

            } else {
                //$this->error('你访问的页面不存在！');
            }

            //文章查看次数自增
            $Article->setInc($id);

            //获得上篇文章
            $pre = $Article->pre($id);

            //获得下篇文章
            $next = $Article->next($id);

            //相关文章
            $relation =  $Article->relation($detail['cat_id']);

            $cat =  D('Category');

            $catTree = $cat->getFamily($detail['cat_id']); //获得家谱树

            $this->assign('catTree',$catTree);
            $this->assign('detail',$detail);
            $this->assign('pre',$pre);
            $this->assign('next',$next);
            $this->assign('relation',$relation);
            $this->display();
        } else {
            $this->error('非法操作！');
        }

    }


    public function tags(){
        $tag        = I('get.tag');
        $cat_id     = I('get.cat_id');
        $Article =  D('Article');
        if (IS_GET) {
            if (!empty($tag)) {
               /********标签搜索********/
                $map['art_tag'] = array('like','%'.$tag.'%');



                $count = $Article->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
                $Page = new Page($count,6);// 实例化分页类 传入总记录数
                // 进行分页数据查询
                $search = M('Article')->articleList($Page,$map);

                $show = $Page->show();// 分页显示输出
                //把表标签  分开 如 '你好,小二'  分成 array('你好','小二');
                $search = $Article->tag($search,$flag = true);
                //调用时间格式化
                $search = $Article->timeFormat($search);

                $this->assign('page',$show);
                $this->assign('keyword',$tag);
                $this->assign('search',$search);
                $this->display('list');


            } else if (!empty($cat_id)){
                /********栏目搜索********/
               $cat = D('Category');
               $ids = $cat->getSonId($cat_id);
               $ids[] = $cat_id;
               $map['blog_article.cat_id'] = array('in',$ids);

                $count = $Article->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件


                $Page = new Page($count,6);// 实例化分页类 传入总记录数
                // 进行分页数据查询
                $search = $Article->search($map,$Page);
                $show = $Page->show();// 分页显示输出

                //把表标签  分开 如 '你好,小二'  分成 array('你好','小二');
                $search = $Article->tag($search,$flag = true);
                //调用时间格式化
                $search = $Article->timeFormat($search);

                $this->assign('page',$show);// 赋值分页输出
                $this->assign('search',$search);
                $this->display('cat');
            }

        } else {
            $this->error('非法操作！');
        }

    }

    public function project() {
        $this->display();
    }

    public function msg(){
        $this->display('msg');
    }

    public function about() {
        $this->display();
    }

    //搜索功能
    public function search() {
        $Article =  D('Article');
        $searched= I('post.s');
        $map['art_content'] = array('like','%'.$searched.'%');


        $count = $Article->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new Page($count,6);// 实例化分页类 传入总记录数
        // 进行分页数据查询
        $search = M('Article')->field('art_id,art_author,art_view,art_title,art_description,pub_time')->limit($Page->firstRow.','.$Page->listRows)->where($map)->select();
        $show = $Page->show();// 分页显示输出

        //把表标签  分开 如 '你好,小二'  分成 array('你好','小二');
        $search = $Article->tag($search,$flag = true);
        //调用时间格式化
        $search = $Article->timeFormat($search);

        $this->assign('page',$show);
        $this->assign('keyword',$searched);
        $this->assign('search',$search);
        $this->display('list');
    }

}